/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




package learn.qual;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.Scanner;

/**
 *
 * @author robit
 */
public class ThemePark {
    /**
    * calculate the answer
    */


    static long reckon(int round, int seats, int[] groupsSet) {
        int grpSize=groupsSet.length;
        int from=0;
        long [] total = new long[grpSize];
        int [] nextIdx = new int[grpSize];
        for (int i = 0; i < grpSize; i++) {
            long sum =0;
            int j=0;
            for (j=0;j<grpSize;j++){
                if(sum+groupsSet[(i+j)%grpSize]>seats){
                    break;
                }
                sum+=groupsSet[(i+j)%grpSize];
            }
            total[i]=sum;nextIdx[i]=(i+j)%grpSize;

        }
        long TOTAL=0;
        int idx=0;
        for(int i =0;i<round;i++){
            TOTAL+=total[idx];
            idx=nextIdx[idx];
        }
        //System.out.println("done:"+boardAllRound);
        return TOTAL;
    }

//    static long reckon(int round, int seats, int[] groupsSet) {
//
//        int mod=groupsSet.length;
//
//        int from=mod-1; // exclusive
//        long boardAllRound=0;
//
//        long [] prefixSum=new long[mod];
//        prefixSum[0]=groupsSet[0];
//        for (int i=1;i<mod;i++){
//            prefixSum[i]=prefixSum[i-1]+groupsSet[i];
//        }
//
//        for (int i = 0; i < round; i++) {
//           // int boardEachRound =0;(a % b + b) % b
//            int to=from; //inclusive
//            int left = (from+1)%mod;//inclusive
//
//            while(true){
//                if(prefixSum[to]-prefixSum[from]+(to<=from?prefixSum[mod-1]:0)<=seats){
//                    break;
//                }else if((left+1)%mod==(to%mod)){
//                    to=left;
//                    break;
//                }else{
//                    //System.out.println("Hi");
//                    int peek = (left+to+(left>to?mod:0))/2%mod;
//                    if(prefixSum[peek]-prefixSum[from]+(peek<=from?prefixSum[mod-1]:0)>seats){
//                        to=peek;
//                    }else{
//                        left=peek;
//                    }
//                }
//            }
//            boardAllRound+=prefixSum[to]-prefixSum[from]+(to<=from?prefixSum[mod-1]:0);
//            from = to;//note this fit as from is exclusive but to is incls
//        }
//        return boardAllRound;
//    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        Scanner s = null;
        PrintWriter o = null;
        try {
            s = new Scanner(
                    new BufferedReader(new FileReader("C-large-practice.in")));
            //s.useLocale(Locale.US);
            o = new PrintWriter(
                    new BufferedWriter(new FileWriter("C-large-practice.out")));

            // Determine input line number
            int lineNums = s.nextInt();

            for (int i = 1; i <= lineNums; i++) {

                // Determine arguments number assign const if fixed
                int Round = s.nextInt();
                int seats = s.nextInt();
                int groupNums = s.nextInt();
                //int argNums = 2;

                // Read all inputs for a line
                //BigInteger argsSet[] = new BigInteger[argNums];
                int groupsSet[] = new int[groupNums];

                // Process a line
                for (int j = 0; j < groupNums; j++) {
                    //groupsSet[j] = s.nextBigInteger();
                    groupsSet[j] = s.nextInt();
                }

                // Calculate answer for a line
                long answer = reckon(Round, seats, groupsSet);
                //int answer = reckon(argsSet);
                o.println("Case #" + i + ": " + answer);
            }
        } finally {
            s.close();
            o.close();
        }
    }
}
